// pages/main/order/addorder.js
import {allconsultlist,addtravelorder} from "../../../api/wrd/index"
Page({
  data: {
    show:false,
    consultlist: [],
    adminId:0,
    peonum:1,
    price:1000,
    allprice:1000,
    name:'',
    tel:'',
    address:'',
    orderdetails:'',
    infosdetail:''
  },
  showPopup(){
    console.log(123112);
    this.setData({show:true})
  },
  //获取顾问列表
  async getconsultlist(){
    let arr=[
      {
        text:"请选择旅游顾问",
        value:0
      }
    ]
    let res=await allconsultlist()
    for(var i in res.dataset){
      console.log(res.dataset[i].username);
      let obj={
        text:res.dataset[i].username,
        value:res.dataset[i].admin_id
      }
      arr.push(obj)
      console.log(arr);
    }
    this.setData({
      consultlist:arr
    
      // adminId:arr[0].value
    })
    console.log("顾问列表",res);
  },
  valuechange(event){
    console.log("下拉框",event);
    this.setData({
      adminId:event.detail
    })
  },
  onClose() {
    this.setData({ show: false });
  },
   onChange(event) {
    this.setData({
      peonum:event.detail,
      allprice:event.detail*this.data.price
    })
    const { picker, value, index } = event.detail;
    // console.log(picker, value, index);
  },
  //获取游客名
  getname(event){
    this.setData({
      name:event.detail.value
    })
  },

  //获取游客电话
  gettel(event){
    this.setData({
      tel:event.detail.value
    })
  },
  //获取结合地点
  getaddress(event){
    this.setData({
      address:event.detail.value
    })
  },
  //确认报名
  async makesure(){
    let data={
      realPrice:this.data.allprice,
      personNum:this.data.peonum,
      userName:this.data.name,
      tel:this.data.tel,
      address:this.data.address,
      adminId:this.data.adminId,
      travelId:wx.getStorageSync('itemDetail').travelId,
      infoId:wx.getStorageSync('itemInfo').infoId,
    }
    console.log(data);
    console.log();
    let res=await addtravelorder(data)
    if(res.dataset.dataset=='成功'){
      console.log(111);
      wx.showToast({
        title: '报名成功',
        icon:'success'
      })
      wx.setStorageSync('travelId',wx.getStorageSync('itemDetail').travelId)
      setTimeout(()=>{
        wx.navigateTo({
          url: '../productDetail/productDetail',
        })
      },800)
      wx.removeStorageSync('itemInfo')
      wx.removeStorageSync('itemDetail')
    }else{
      console.log(222);
      wx.showToast({
        title: res.dataset.dataset,
        icon:"none"
      })
    }
    console.log("添加旅行信息",res);
  },
  onLoad(options) {
    this.getconsultlist()
    this.setData({
      orderdetails:wx.getStorageSync('itemDetail'),
      infosdetail:wx.getStorageSync('itemInfo'),
      price:wx.getStorageSync('itemDetail').price
    })
    // console.log(111);
    // console.log(wx.getStorageSync('itemInfo'));
    // console.log(wx.getStorageSync('itemDetail'));
    // wx.getStorageSync('')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})